package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.MenuDto;
import com.itheima.ydd.dto.MenuDtos;
import com.itheima.ydd.dto.RoleDto;
import com.itheima.ydd.dto.RoleInfo;
import com.itheima.ydd.entity.Menu;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.service.MenuService;
import com.itheima.ydd.service.RoleMenuService;
import com.itheima.ydd.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2021-2022
 * Author: 16372
 * Date: 2022/2/26 15:14
 * FileName: RoleController
 * Description: 角色管理
 */
@RestController
@RequestMapping("/sys_role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private MenuService menuService;

    /**
     * @Author 16372
     * @Description //TODO 角色分页列表模糊查询
     * @Date 15:40 2022/2/26
     * @Param [page, limit, name]
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/list")
    public ResultData getList(Integer page,Integer limit,String name){
        Page<Role> rolePage = new Page<>(page,limit);
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name != null,Role::getName,name);
        roleService.page(rolePage,lambdaQueryWrapper);
        return ResultData.ok("", PageResult.init(rolePage));
    }

    /**
     * @Author 16372
     * @Description //TODO 根据id查询角色详情
     * @Date 15:44 2022/2/26
     * @Param [id]
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        //传来的是roleId
        Role role = roleService.getById(id);
        RoleDto roleDto = new RoleDto();
        BeanUtils.copyProperties(role,roleDto);

        //查到所有的role,根据role查到所有的menu_id
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRole_id, id);
        List<SysRoleMenu> roleMenulist = roleMenuService.list(queryWrapper);

        List<Integer> menuIdList = roleMenulist.stream().map(s -> {
            Integer menu_id = s.getMenu_id();
            return menu_id;
        }).collect(Collectors.toList());


        //查到pid为0的 menu
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.in(Menu::getId,menuIdList);
        menuLambdaQueryWrapper.eq(Menu::getPid,0);
        List<Menu> list = menuService.list(menuLambdaQueryWrapper);

        List<MenuDtos> menuDtoList = list.stream().map(s -> {
            MenuDtos menuDto = new MenuDtos();
            BeanUtils.copyProperties(s, menuDto);
            return menuDto;
        }).collect(Collectors.toList());

        //封装完毕的menudto
        List<MenuDtos> MenuDto = digui(menuDtoList, menuIdList);
        roleDto.setAuthList(MenuDto);


        return ResultData.ok("", roleDto);

    }
    
    /**
     * @Author 16372
     * @Description //TODO 角色修改
     * @Date 15:46 2022/2/26
     * @Param [role]
     * @return com.itheima.ydd.common.ResultData
     **/
    @PostMapping("/update")
    public ResultData update(@RequestBody Role role){
        roleService.updateById(role);
        return ResultData.ok("","");
    }

    /**
     * @Author 16372
     * @Description //TODO 添加角色
     * @Date 21:53 2022/3/2
     * @Param [roleInfo]
     * @return com.itheima.ydd.common.ResultData
     **/
    @PostMapping("/add")
    public ResultData add(@RequestBody RoleInfo roleInfo){
        System.out.println("roleInfo = " + roleInfo.getMenuids());
        Role role = new Role();
        role.setName(roleInfo.getName());
        role.setDescription(roleInfo.getDescription());
        roleService.save(role);
        ArrayList<Integer> menuids = roleInfo.getMenuids();
        for (Integer menuid : menuids) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRole_id(role.getId());
            sysRoleMenu.setMenu_id(menuid);
            roleMenuService.save(sysRoleMenu);
        }
        return ResultData.ok("","");
    }

    @GetMapping("/del")
    public ResultData delect(String id){
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRole_id,id);
        roleMenuService.remove(queryWrapper);
        roleService.removeById(id);
        return ResultData.ok("","");
    }

    public List<MenuDtos> digui(List<MenuDtos> dtoList, List<Integer> menuIdList){
        List<MenuDtos> collect = dtoList.stream().map(s -> {
            Integer id = s.getId();
            LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            menuLambdaQueryWrapper.eq(Menu::getPid, id);
            menuLambdaQueryWrapper.in(Menu::getId,menuIdList);
            List<Menu> list = menuService.list(menuLambdaQueryWrapper);
            if (list.size()==0) {
                return s;
            }

            List<MenuDtos> menuDtoList_angin = list.stream().map(s1 -> {
                MenuDtos menuDto = new MenuDtos();
                BeanUtils.copyProperties(s1, menuDto);
                return menuDto;
            }).collect(Collectors.toList());

            s.setChildren(menuDtoList_angin);

            digui(menuDtoList_angin,menuIdList);

            return s;

        }).collect(Collectors.toList());

        return collect;
    }

}
